<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="货号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色" prop="colorProperty">
        <el-input
          v-model="queryParams.colorProperty"
          placeholder="请输入颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="异常" prop="isAbnormal">
        <el-select
          v-model="queryParams.isAbnormal"
          clearable
          placeholder="是否异常"
          filterable
        >
          <el-option label="正常" value="0" />
          <el-option label="异常" value="1" />
        </el-select>
        <!-- <el-input 0正常 1异常
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
        <el-input v-model="queryParams.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="display: none">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:procode:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:procode:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5" style="display: none">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:procode:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:procode:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" plain icon="el-icon-upload" @click="daoru"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefresh"
          >同步</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleupdateAbnormalAllSkc"
          >一键修改异常skc</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleupdateJstSkc"
          >批量修改异常skc</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button plain size="mini" @click="handleselectAbnormalAllIds"
          >一键获取异常数据id</el-button
        >
      </el-col> -->

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="procodeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编码ID" align="center" prop="id" /> -->

      <el-table-column label="货号" align="center" prop="productNo" />
      <el-table-column label="颜色" align="center" prop="colorProperty" />
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="聚水潭Skc" align="center" prop="jstSkc" />
      <el-table-column label="是否异常" align="center" prop="isAbnormal">
        <template slot-scope="scope">
          {{ scope.row.isAbnormal | state1 }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="逻辑删除;0=未删除；1=已删除" align="center" prop="isDelete" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:procode:edit']"
            >修改</el-button
          >
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['product:procode:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品编码数据字典对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货号" prop="productNo">
          <el-input
            v-model="form.productNo"
            placeholder="请输入货号"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="颜色" prop="colorProperty">
          <el-input
            v-model="form.colorProperty"
            placeholder="请输入颜色"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" />
        </el-form-item> -->
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改产品二级信息对话框 :on-change="handleChange"-->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="500px"
      append-to-body
    >
      <el-upload
        :action="Upload.url"
        :headers="Upload.headers"
        :limit="limit"
        :on-exceed="handleExceed"
        :on-success="handlePictureSuccessPic"
        :before-upload="beforeAvatarUpload"
        :on-preview="handlePictureCardPreview"
        :file-list="fileLi"
        :on-change="changeFile1"
      >
        <el-button size="small" type="primary">导入文件</el-button>
        <!-- <i class="el-icon-plus"></i> -->
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProcode,
  getProcode,
  delProcode,
  addProcode,
  updateProcode,
  updateAbnormalAllSkc,
  jstSync,
  updateJstSkc,
  selectAbnormalAllIds,
} from "@/api/data/procode";
//--------导入token
import { getToken } from "@/utils/auth";
export default {
  name: "Procode",
  data() {
    return {
      list: [],
      // 图片数量限制
      limit: 2,
      dialogVisible: false,
      fileLi: [],
      file: "",
      Upload: {
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        // 图片上传的方法地址:
        url: process.env.VUE_APP_BASE_API + "/product/procode/importProCode",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品编码数据字典表格数据
      procodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: null,
        productNo: null,
        colorProperty: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [
          { required: true, message: "产品编码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  filters: {
    state1(string) {
      if (string == "0") {
        return "正常";
      }
      if (string == "1") {
        return "异常";
      }
      // if (string == "VFU") {
      //   return "VFU";
      // }
      // if (int == 2) { return "审批通过" };
    },
  },
  created() {
    this.getList();
  },
  methods: {
    handleupdateJstSkc() {
      if (this.ids.length < 1) {
        this.$modal.msgError("至少选择一条数据");
        return;
      }
      this.openLoading();
      updateJstSkc(this.list).then(() => {
        this.$modal.msgSuccess("已批量修改");
        this.$modal.closeLoading();
        this.getList();
      });
    },
    handleselectAbnormalAllIds() {
      this.openLoading();
      selectAbnormalAllIds().then(() => {
        // this.$modal.msgSuccess("同步数据成功");
        this.$modal.closeLoading();
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    /** -键修改异常skc Warning*/
    handleupdateAbnormalAllSkc() {
      // this.$modal.msgWarning("数据正在同步，请稍候");

      // const obj = {
      //   startTime: this.timeGap.startTime,
      //   endTime: this.timeGap.endTime,
      // };
      this.openLoading();
      updateAbnormalAllSkc().then(() => {
        // this.$modal.msgSuccess("同步数据成功");
        this.$modal.closeLoading();
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载数据，请稍候！");
    },
    /** 刷新缓存按钮操作 Warning*/
    handleRefresh() {
      // this.$modal.msgWarning("数据正在同步，请稍候");

      // const obj = {
      //   startTime: this.timeGap.startTime,
      //   endTime: this.timeGap.endTime,
      // };
      this.openLoading();
      jstSync().then(() => {
        this.$modal.msgSuccess("同步数据成功");
        this.$modal.closeLoading();
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    //部门文件导入修改打开
    daoru() {
      this.dialogVisible = true;
    },
    /** 查询产品编码数据字典列表 */
    getList() {
      this.loading = true;
      listProcode(this.queryParams).then((response) => {
        this.procodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productCode: null,
        productNo: null,
        colorProperty: null,
        isDelete: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.list = selection.map((item) => item);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品编码数据字典";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProcode(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品编码数据字典";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProcode(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcode(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品编码数据字典编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProcode(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "product/procode/export",
        {
          ...this.queryParams,
        },
        `procode_${new Date().getTime()}.xlsx`
      );
    },
    //图片上传前的相关判断
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg' || file.type == 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 5;
      // if (!isJPG) {
      //     this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
      // }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
      }
      return isLt2M;
    },
    //图片预览
    handlePictureCardPreview(file) {
      // this.picUrl = file.url
      // this.imageUrl = file.url;
      // this.dialogVisible = true;
    },
    //
    // handleChange(file, fileList) {
    //     if (fileList.length > 0) {
    //         this.fileLi = [fileList[fileList.length - 1]]
    //     }
    //     // }
    // },

    //文件导入成功回传

    handlePictureSuccessPic(res, file) {
      console.log(JSON.stringify(res));
      //设置图片访问路径 （url 后台传过来的的上传地址）
      // this.picUrl = file.response.url;
      // const url = file.response.fileName;
      this.file = file.response.url;
      console.log(JSON.stringify(this.file));
      if (res.code == 200) {
        this.$modal.msgSuccess("导入成功");
        this.dialogVisible = false;
        this.$modal.msgError(res.msg);
        this.getList();
        return true;
      } else {
        this.$modal.msgError(res.msg);
        this.dialogVisible = true;
        // this.$message.msgError('该颜色未在字典定义')
        return;
      }
    },
    // //图片上传成功后的回调
    // handlePictureSuccess(res, file) {
    //     //设置图片访问路径 （url 后台传过来的的上传地址）
    //     this.imageUrl = file.response.url;
    //     // const url = file.response.fileName;
    //     this.logoUrl = file.response.fileName;
    //     // console.log(JSON.stringify(url));
    // },

    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传链接文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传文件让第二次覆盖第一的文件
    changeFile1(file, fileList) {
      if (fileList.length > 1) {
        this.fileLi = fileList.slice(-1);

        // this.fileList = [fileList[fileList.length - 1]]//这一步，是 展示最后一次选择文件
      }
      // this.fileSpecial = fileList;
    },
  },
};
</script>
